import Vue from 'vue'
import Router from 'vue-router'

// 路由懒加载
const login = () => import('../views/login/login.vue')
const home = () => import('../views/home/home.vue')
const welcome = () => import('../views/home/components/welcome.vue')
const theaterManage = () => import('../views/theaterManage/theaterManage.vue')
const showManage = () => import('../views/showManage/showManage.vue')
const userManage = () => import('../views/userManage/usermanage.vue')
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    meta: {
      isAlive: true
    },
    redirect: '/welcome',
    children: [
      {
        // 首页概况
        path: '/welcome',
        meta: {
          isAlive: true
        },
        component: welcome
      },
      {
        // 剧院管理
        path: '/theaterManage',
        // 剧院管理页面不缓存
        meta: {
          isAlive: false
        },
        component: theaterManage
      },
      { // 剧目管理
        path: '/showManage',
        meta: {
          isAlive: false
        },
        component: showManage
      },
      { // 用户管理
        path: '/userManage',
        meta: {
          isAlive: false
        },
        component: userManage
      }
    ]
  }
]
Vue.use(Router)

const router = new Router({
  mode: 'hash',
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to是将要访问的路径
  // from是哪个路径跳转过来
  // next是放行函数 2种使用 next() next('/login')
  // console.log(window.location.pathname) 显示路由的path
  if (to.path === '/login') return next()
  if (to.path === from.path) {
    console.log(from.path)
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
